﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureProvider.ssdsClient;
using BlogEngine.Core.DataStore;
using System.Xml.Serialization;
using System.IO;

namespace AzureProvider.SoupMethods
{

    public class DataStoreSetting
    {
        //ExtensionType, ExtensionId, Settings
        public string Id;
        public int ExtensionType;
        public string ExtensionId, Settings;

    }
    public class DataStoreSettingsMethods : BaseMethods 
    {

        private string errMessage;

        protected override string containerId
        {
            get { return "DataStoreSettings"; }
        }

        protected override string kind
        {
            get { return "DataStoreSetting"; }
        }

        protected override Entity CreateEntity(object Object)
        {
            DataStoreSetting myobj = (DataStoreSetting)Object;

            var entity = new Entity()
            {
                Id = myobj.Id,
                Kind = kind
            };

            entity.Properties = new Dictionary<string, object>();
            entity.Properties.Add("ExtensionType", myobj.ExtensionType.ToString());
            entity.Properties.Add("ExtensionId", myobj.ExtensionId);
            entity.Properties.Add("Settings", myobj.Settings);

            return entity;
        }

        public override object CreateObject(Entity Entity)
        {
            DataStoreSetting myObj = new DataStoreSetting();
            myObj.Id = Entity.Id;
            myObj.ExtensionType = int.Parse((string)Entity.Properties.Single(tt => tt.Key == "ExtensionType").Value);
            myObj.ExtensionId = (string)Entity.Properties.Single(tt => tt.Key == "ExtensionId").Value;
            myObj.Settings = (string)Entity.Properties.Single(tt => tt.Key == "Settings").Value;

            return myObj;
        }

        protected virtual List<Entity> GetEntities(ExtensionType exType, string exId)
        {
            string errMessage;
            var qry = "from e in entities where e.Kind == \"" + kind + "\" && e[\"ExtensionType\"]==\"" + exType.GetHashCode() + "\" && e[\"ExtensionId\"]==\"" + exId + "\"  select e";
            List<Entity> ents = GetEntities(qry, out errMessage);
            return ents;
        }

        public  object LoadFromDataStore(ExtensionType exType, string exId)
        {
            List<Entity> ents = GetEntities(exType, exId);
            if (ents.Count > 0)
            {
                return ((DataStoreSetting)CreateObject(ents.First())).Settings ;
            }
            else
            {
                return null;
            }
        }
        public void SaveToDataStore(ExtensionType exType, string exId, object settings)
        {
             RemoveFromDataStore(exType,  exId);

             XmlSerializer xs = new XmlSerializer(settings.GetType());
             string objectXML = string.Empty;
             using (StringWriter sw = new StringWriter())
             {
                 xs.Serialize(sw, settings);
                 objectXML = sw.ToString();
             }

            Insert( new DataStoreSetting{
                Id = Guid.NewGuid().ToString(),
                ExtensionType = exType.GetHashCode(),
                ExtensionId = exId,
                Settings = objectXML
            },out errMessage);
        }
        public void RemoveFromDataStore(ExtensionType exType, string exId)
        {
            List<Entity> ents = GetEntities(exType, exId);
            if (ents.Count > 0)
            {
                 Delete(ents.First().Id);
            }
            else
            {
               
            }
        }
    }
}
